
<video autoplay id="sourcevid" style="width:320px;height:240px"></video>

<button id="snap">拍照</button>

    <canvas id="output" style="display:none" width="320" height="240"></canvas>

    <script type="text/javascript" charset="utf-8">

        // var socket = new WebSocket("ws://"+document.domain+":8080");
        var back = document.getElementById('output');
        var backcontext = back.getContext('2d');
        var video = document.getElementsByTagName('video')[0];
        
        var success = function(stream){
            video.src = window.URL.createObjectURL(stream);
        }

       /*  socket.onopen = function(){
            draw();
        } */

        var draw = function(){
            try{
                backcontext.drawImage(video,0,0, back.width, back.height);
            }catch(e){
                if (e.name == "NS_ERROR_NOT_AVAILABLE") {
                    return setTimeout(draw, 100);
                } else {
                    throw e;
                }
            }
            // socket.send(back.toDataURL("image/jpeg", 0.5));
            setTimeout(draw, 100);
        }
        navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
        navigator.mozGetUserMedia || navigator.msGetUserMedia;
        navigator.getUserMedia({video:true, audio:false}, success, console.log);
        
        $(function() {
        	$("#snap").click(function() {
    			// context.drawImage(video, 0, 0, 320, 320);
    			backcontext.drawImage(video, 0, 0, back.width, back.height);
    			// backcontext.drawImage(video, 0, 0, 320, 240);
    			
    			 // var imgData = back.toDataURL();
    			 var imgData = back.toDataURL("image/jpeg");
    			//将图像转换为base64数据
    			 var base64Data = imgData.substr(22);
    			 console.log(base64Data);
    			 
    			 var body = {};
					body.img = base64Data;
    			 
    			 $.post('emp.php?do=Emp.ajax_photo', body, function(data) {
    				 alert($.toJSON(data));
					// $(result).html(data);
				});
    		});
        });
    </script>

